# coding: utf8
# -*- coding: utf-8 -*-  
#author: Cullen

"""
设置笔记本为热的步骤:
1.以管理员身份启动cmd
2.启动无线虚拟网卡命令如下:
    netsh wlan set hostednetwork mode=allow ssid=(这里写无线网名字) key=(这里是密码)
    netsh wlan set hostednetwork mode=allow ssid=zsl1 key=zsl000000
3.到设备管理器中启动虚拟网卡适配器, 网络中心会生成一个新的连接
4.到网络中心, 将本地网络共享给新建的这个连接步骤如下:
    本地连接-->属性-->共享选项卡-->选择允许其他网络用户...连接-->选择新生成的连接-->确定
5.设置新连接的DNS和ip地址
    一般设为: 192.168.2.1
6.启动热点
    netsh wlan start hostednetwork
    netsh wlan stop hostednetwork 关闭热点


"""
  
#import the needed module  
from Tkinter import *  
import tkFont  
import tkMessageBox  
import os  
from PIL import ImageTk, Image

  
def close_window(window):  
    """give prompt when user close the window"""  
    if tkMessageBox.askyesno("QUIT", "Close the Window(Yes/No)", icon="question"):  
        window.destroy()   
      
def manage_wifi(how, message_frame):  
    """this function will open/close the wifi network accoring the arguments: 
        how=1(open the network) how=2(close the network), 
        message_frame(the frame which the message display"""  
      
    open_wifi_cmd = "netsh wlan start hostednetwork"  
    close_wifi_cmd = "netsh wlan stop hostednetwork"  
      
    if how == 1:  
        cmd = open_wifi_cmd  
    else:  
        cmd = close_wifi_cmd  
  
    result = os.system(cmd)  
    if result != 0:  
        if how == 1:  
            message_frame.listbox_insert("请检查无线网卡是否打开，设置是否正确")  
        else:  
            message_frame.listbox_insert("关闭WIFI失败！")  
    else:  
        if how == 1:  
            message_frame.listbox_insert("WIFI已打开")  
        else:  
            message_frame.listbox_insert("WIFI已关闭")  
  
class ShowMessageFrame():  
    """will create a frame contanis a listbox and scrollbar"""  
      
    def __init__(self):  
        self.frame = Frame()  
        self.message_ft = tkFont.Font(family="Arial", size=10)  
        self.scrollbar = Scrollbar(self.frame, orient=VERTICAL)  
        self.listbox = Listbox(self.frame, bg="grey", selectbackground="blue",  
                               selectmode="extended", font=self.message_ft, width=20)  
        self.scrollbar.config(command=self.listbox.yview)  
        self.scrollbar.pack(side=RIGHT, fill=Y)  
        self.listbox.config(yscrollcommand=self.scrollbar.set)  
        self.listbox.pack(side=LEFT, fill=BOTH, expand=1)  
  
        self.listbox_insert("Welcome to WIFI!")  
  
    def listbox_insert(self, args):  
        self.listbox.insert(END, args)  
  
class MyMenu():  
    """Create the Menu for Window"""  
    message_status = 1  
    def __init__(self, root): 
        self.root = root
        self.menubar = Menu(self.root)
        self.add_optionmenu() 
        self.add_helpmenu()
        self.frame = ''

    def add_helpmenu(self):
        """增加帮助菜单"""
        helpmenu = Menu(self.menubar, tearoff=1)
        helpmenu.add_command(label='About', command=self.show_info)  
        self.menubar.add_cascade(label='Help', menu=helpmenu)  

    def add_optionmenu(self):
        """增加主菜单"""
        optionmenu = Menu(self.menubar, tearoff=1)
        optionmenu.add_command(label='Show Message', command=self.show_messagebox)  
        optionmenu.add_command(label='Hide Message', command=self.hide_messagebox)  
        optionmenu.add_separator()  
        optionmenu.add_command(label='Exit', command=lambda : close_window(self.root))  
        self.menubar.add_cascade(label='Options', menu=optionmenu)
  
    def hide_messagebox(self,):  
        """will hide the message listbox and scrollbar"""  
        if self.message_status == 1:  
            self.frame = self.root.grid_slaves(2,0)[0]  
            self.root.grid_slaves(2,0)[0].grid_remove()  
            self.__class__.message_status = 0  
  
    def show_messagebox(self):  
        if self.message_status == 0 and self.frame != '':  
            #print root.grid_slaves(2,0)  
              
            self.frame.grid(row=2, columnspan=2, pady=5, sticky=S+N+E+W)  
            self.__class__.message_status = 1  
            self.frame = ''  

    @staticmethod
    def show_info():  
        """show the software info"""   
        tkMessageBox.showinfo("About",  
                              """ 
                    WIFI 热点助手 
        ------------------------------------------ 
                version: 1.0 
                author: Cullen 
                Email:  wangyiyan402@163.com 
        ------------------------------------------""" )  
            
      
def run():  
    root = Tk()  
    root.title("WIFI热点助手");  
    # root.resizable(0,0)  
    
    #create frame which include listbox and scrollbar  
    message_frame = ShowMessageFrame()  
    message_frame.frame.grid(row=2, columnspan=2, pady=5, sticky=S+N+E+W)  
    #message_frame.frame.grid_forget() 

    #create menu  
    my_menu = MyMenu(root)  
    root.config(menu=my_menu.menubar)  

    #font and image  
    image = Image.open("../wifi.jpg")  
    bm = ImageTk.PhotoImage(image)  
      
    label = Label(root, image=bm)  
    label.grid(row=0, columnspan=2)  

    button_ft = tkFont.Font(family="Arial", size=10, weight=tkFont.BOLD)  
    #create button  
    open_button = Button(root, text="OPEN", 
                            font=button_ft, pady=10,  
                            width=10, borderwidth=2, bg="#F3E9CC", 
                            command=lambda : manage_wifi(1, message_frame))  
    open_button.grid(row=1, column=0)  

    close_button = Button(root, text="CLOSE", 
                            font=button_ft, pady=10,  
                            width=10, borderwidth=2, bg="#F3E9CC", 
                            command=lambda : manage_wifi(0, message_frame))  
    close_button.grid(row=1, column=1)  

    #prompt when user close the window  
    root.protocol('WM_DELETE_WINDOW', lambda : close_window(root))  
    root.mainloop()  
  
if __name__ == '__main__':  
    #main()  
    run()